package com.fany.fastsearcher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.fany.constants.StringResources;
import com.fany.fastsearcher.R;
import com.fany.utils.CommonUtils;
import com.fany.utils.FileHelper;
import com.fany.views.controls.ListViewAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by FANY on 2015/2/2.
 */
public class FileSelectActivity extends ListViewActivity
{
    private ArrayList<HashMap<String, Object>> _items;
    private static final String rootPath = "/";
    private String _path = rootPath;
    private ListViewAdapter _adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Button buttonConfirm = (Button) findViewById(R.id.fileselect_button_confirm);
        buttonConfirm.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Intent data = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("path", _path);
                data.putExtras(bundle);
                setResult(RESULT_OK, data);
                finish();
            }
        });

        Button buttonCancel = (Button) findViewById(R.id.fileselect_button_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                finish();
            }
        });

        loadFiles();
    }



    private void openFile()
    {
        String path = _items.get(_adapter.getSelectedIndex()).get("path").toString();
        _path = path;
        loadFiles();
    }

    private void loadFiles()
    {
        if (!FileHelper.isDirectory(_path))
        {
            _path = FileHelper.extractDirectoryName(_path);
        }
        _items = new ArrayList<HashMap<String, Object>>();
        if (!_path.equals(rootPath))
        {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("name", "..");
            String path = FileHelper.extractDirectoryName(_path);
            if (path.isEmpty())
            {
                path = rootPath;
            }
            item.put("path", path);
            item.put("image", R.drawable.upward);
            _items.add(item);
        }
        List<String> rets = FileHelper.enumFiles(_path);

        for (String name : rets)
        {
            File file = new File(FileHelper.combineFileName(_path, name));
            if (!file.isDirectory())
            {
                continue;
            }
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("name", name);
            item.put("path", FileHelper.combineFileName(_path, name));
            item.put("image", R.drawable.folder);
            _items.add(item);
        }
        _adapter = new ListViewAdapter(this, _items, R.layout.filelist_liststyle, new String[]{
                "name", "image",
                "path"
        }, new int[]{
                R.id.filelist_liststyle_content, R.id.filelist_liststyle_icon,
                R.id.filelist_liststyle_path
        });

        getListView().setAdapter(_adapter);
    }

    private void updateSelectedIndex(int index)
    {
        if (_adapter == null)
        {
            return;
        }
        _adapter.setSelectedIndex(index);
        _adapter.notifyDataSetChanged();
        setPosition(index);
    }

    @Override
    protected int getListViewID()
    {
        return R.id.fileselect_listview;
    }

    @Override
    protected int getContentViewID()
    {
        return R.layout.fileselect_activity;
    }

    @Override
    protected void onItemClick(int index)
    {
        updateSelectedIndex(index);
        openFile();
    }
}
